//
// This file is part of cpp-lib, a library of C++ functions and classes.
// Cpp-lib is copyright (C) 2004 and onwards, The CPL Interest Group.
// 
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or (at
// your option) any later version.
// 
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
// General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with this library (see the file COPYING); if not, write to the
// Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
// 02111-1307  USA
//
// $Id$
//

#include <iostream>
#include <list>
#include <vector>

#include <cstdlib>

#include "cpp-lib/util.h"
#include "cpp-lib/container-util.h"


using namespace cpl::util            ;
using namespace cpl::util::container ;

void testChop(int i)
{
	std::string s("...X ");
    std::size_t n = s.size();
	s[3] = i;
	cpl::util::chop(s);
	if (i == ' ' || i == '\t' || i == '\n' || i == '\v' || i == '\f' || i == '\r')
		always_assert(s.size() == n-2);
	else
		always_assert(s.size() == n-1);
}


template< typename C >
void check_ascending() {

  C l ;
  l.clear() ;
  l.push_back( 0  ) ;
  l.push_back( 1  ) ;
  l.push_back( -1 ) ;

  bool ok = false ;

  try { check_strictly_ascending( l.begin() , l.end() ) ; }
  catch( std::exception const& e ) {

	ok = true ;
	std::cout << "check_strictly_ascending() works: " << e.what() << '\n' ;

  }

  if( !ok ) {

	std::cout << "check_strictly_ascending() failed\n" ;

  }


}

template< typename C >
void check_iterator() {

  typedef typename C::const_iterator ci ;

  std::cout << "iterator advance:\n" ;

  C l( 10 ) ;

  ci i = l.begin() ;

  while( ( i = safe_advanced( i , ci( l.end() ) , 2 ) ) != l.end() ) 
  { std::cout << std::distance( ci( l.begin() ) , i ) << '\n' ; }

  check_ascending< C >() ;

}

int main()
{
	{
		int arr[20];
		int *poi;
		always_assert(cpl::util::size(arr) == sizeof(arr)/sizeof(arr[0]));
		always_assert(cpl::util::size(poi)==1);
	}

	{
		for (int i=1; i < 256; ++i)
			testChop(i);
	}

	std::cout << "check_iterator< std::list  < int > >()\n" ;
	check_iterator< std::list  < int > >() ;

	std::cout << "check_iterator< std::vector< int > >()\n" ;
	check_iterator< std::vector< int > >() ;

	{
		always_assert(2+2==4);
		always_assert(2+2==5);		// should fail
		/*NOTREACHED*/
	}
}

